<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="/js/axios-1.10.0.js"></script>
</head>
<body>
    <h1>OAuth2</h1>

    <hr>
    <hr>
    <hr>
    <button onclick="createOatuh2Client()">创建客户端</button>
    <br>
    <br>
    <button onclick="getOatuh2Client()">查询客户端</button>
    <br>
    <br>
    <button onclick="updateOatuh2Client()">更新客户端</button>
    <br>
    <br>
    <button onclick="deleteOatuh2Client()">删除客户端</button>

    <script>
        // 创建当前登录用户的 API 客户端
        function createOatuh2Client() {
            // 这个 redirect 跳转有什么用，测试中没看到效果。
            // 后来明白了：这是创建客户端，redirect 是在后续的 授权 时才有效果。
            const data = {
                name: 'tp505-client',
                redirect: 'http://www.tp505.cm/index/auth/callback'
            };

            axios.post('/oauth/clients', data)
                .then(response => {
                    console.log(response.data);
                    /*
                    created_at: "2025-07-24T17:19:12.000000Z"
                    id: 4
                    name: "tp505-client"
                    password_client: false
                    personal_access_client: false
                    provider: null
                    redirect: "http://www.laravel7120.cm/callback"
                    revoked: false
                    secret: "CbG08Sa4wWNcfit8OzJcFCBk3RXw6dlbnnRK1F0m"
                    updated_at: "2025-07-24T17:19:12.000000Z"
                    user_id: 2
                    */
                })
                .catch (response => {
                    console.error(response);
                });
        }

        function getOatuh2Client() {
            axios.get('/oauth/clients')
                .then(response => {
                    console.log(response.data);
                })
                .catch (response => {
                    console.error(response);
                });
        }

        function updateOatuh2Client() {
            const data = {
                name: 'tp505-client-2',
                redirect: 'http://www.laravel7120.cm/callback'
            };

            axios.put('/oauth/clients/' + clientId, data)
                .then(response => {
                    console.log(response.data);
                })
                .catch (response => {
                    console.error(response);
                });
        }

        function deleteOatuh2Client() {
            axios.delete('/oauth/clients/' + clientId)
                .then(response => {
                    console.log(response.data);
                })
                .catch (response => {
                    console.error(response);
                });
        }
    </script>
</body>
</html>